import React from "react";
import { Button, message, Popconfirm, Modal } from "antd";
import { transcode } from "../../services/fetch";
import { connect } from "dva";
import { addArticleOrImages, addVideoOrAudio, editInfoSave, editVideoSave } from "../../services/services";
import { throttle } from "../../utils/Date";
import "./Footer.less";

let adding = false;
/**
 * 连续点击判断
 */
const getAdding = () => {
  return adding;
};
const setAdding = (val, time) => {
  if (time > 0) {
    setTimeout(() => {
      window.close();
      adding = val;
    }, time);
  } else {
    adding = val;
  }
};
@connect(({ add }) => ({
  add: add
}))
class Footer extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      adding: false,
      user_name: "",
      add_information_id: 0
    };
  }

  componentDidMount() {}
  /**
   * 表单重置
   */
  reSet = () => {
    this.props.reset();
  };

  confirm = () => {
    window.close();
  };

  /**
   * 保存草稿
   */
  submitForm = idss => {
    let leftForm = this.props.getLeft();
    let rightForm = this.props.getRight();
    let allForm = {};
    let getUrl = window.location.search;
    let strr = getUrl.substring(1, getUrl.length);
    let arrStr = strr.split("&");
    let level = 0;
    arrStr.map((item, index) => {
      let nameArr = item.split("=");
      if (nameArr[0] === "level") {
        level = Number(nameArr[1]);
      }
    });
    if (leftForm && rightForm) {
      if (getAdding()) {
        return false;
      }
      setAdding(true, 0);
      Object.assign(allForm, leftForm, rightForm);
      if (this.props.type === "1" || this.props.type === "2") {
        const params = {
          content_type: Number(this.props.type),
          content_id: this.props.add.informationInfo.id || "",
          title: allForm.information_title || "",
          content: allForm.content || "",
          receive_site: allForm.receive_site,
          img: allForm.information_img || "",
          summary: allForm.intro || "",
          url: allForm.url || "",
          reporter: allForm.reporter || "",
          eidtor: allForm.editor || "",
          resource: allForm.resource || "",
          is_comment: allForm.is_comment,
          is_check_comment: allForm.is_check_comment,
          is_reprint: allForm.is_original,
          keyword: allForm.key_word,
          default_scan_count: allForm.default_scan_num,
          default_like_count: allForm.default_thumbs_up_num,
          default_share_count: allForm.default_forward_num,
          forward_coefficient: allForm.forward_coefficient,
          scan_coefficient: allForm.scan_coefficient,
          thumbs_up_coefficient: allForm.thumbs_up_coefficient,
          original_intro: allForm.original_intro,
          img_arr: allForm.img_arr || [],
          level: level
        };
        this.props.add.informationInfo.title ? this.editInfo(params) : this.addInfo(params);
      } else {
        let url = this.props.type === "4" ? allForm.video_src : allForm.audio_src;
        const params = {
          content_type: Number(this.props.type),
          content_id: this.props.add.videoInfo.id || "",
          title: allForm.video_title,
          receive_site: allForm.receive_site,
          img: allForm.information_img || "",
          summary: allForm.video_intro || "",
          url: url || "",
          keyword: allForm.key_word,
          reporter: allForm.reporter || "",
          eidtor: allForm.editor || "",
          resource: allForm.resource || "",
          is_comment: allForm.is_comment,
          is_check_comment: allForm.is_check_comment,
          is_reprint: allForm.is_original,
          default_scan_count: allForm.default_scan_num,
          default_like_count: allForm.default_thumbs_up_num,
          default_share_count: allForm.default_forward_num,
          forward_coefficient: allForm.forward_coefficient,
          scan_coefficient: allForm.scan_coefficient,
          thumbs_up_coefficient: allForm.thumbs_up_coefficient,
          original_intro: allForm.original_intro,
          level: level
        };
        this.props.add.videoInfo.title ? this.editVideo(params) : this.addVideo(params);
      }
    }
  };
  // 添加文章和图集
  addInfo = params => {
    addArticleOrImages(params)
      .then(res => {
        if (res.code === 200) {
          message.success("添加成功");
          window.localStorage.setItem('jg_add',new Date())
          this.closeWindow();
        } else {
          console.log(res.msg);
        }
      })
      .catch(err => {
        console.log(err);
      });
  };
  // 添加视频和音频
  addVideo = params => {
    addVideoOrAudio(params)
      .then(res => {
        if (res.code === 200) {
          message.success("添加成功");
          window.localStorage.setItem('jg_add',new Date())
          this.closeWindow();
        } else {
          console.log(res.msg);
        }
      })
      .catch(err => {
        console.log(err);
      });
  };
  // 编辑文章和图集
  editInfo = params => {
    editInfoSave(params)
      .then(res => {
        if (res.code === 200) {
          message.success("更新成功");
          window.localStorage.setItem('jg_edit',new Date())
          this.closeWindow();
        }else {
          console.log(res.msg);
        }
      })
      .catch(err => {
        console.log(err);
      });
  };
  // 编辑视频和音频
  editVideo = params => {
    editVideoSave(params)
      .then(res => {
        if (res.code === 200) {
          message.success("更新成功");
          window.localStorage.setItem('jg_edit',new Date())
          this.closeWindow();
        } else {
          console.log(res.msg);
        }
      })
      .catch(err => {
        console.log(err);
      });
  };
  closeWindow = () => {
    message.success("1秒后自动关闭页面~");
    setTimeout(() => {
      window.close();
    }, 1000);
  };
  //提交转码任务
  submitTranscode = (postData, id, type) => {
    if (postData && postData.length > 0) {
      postData.forEach(item => {
        item.article_id = item.video_id;
        item.type = 1;
        if (type === 2) {
          item.information_id = id;
        }
        if (type === 3) {
          item.theme_information_id = id;
        }
        transcode(item);
      });
      this.props.dispatch({
        type: "add/setTranscodeData",
        data: []
      });
    }
  };

  render() {
    let size = "large";
    return (
      <div className="Footer">
        <Button size={size} type="primary" onClick={throttle(this.submitForm, 1000)}>
          保存草稿
        </Button>
        <Popconfirm className="FooterPop" placement="topLeft" icon={null} title="确认要重置吗?" onConfirm={this.reSet} onCancel={() => {}} okText="确定" cancelText="取消">
          <Button size={size}>重置</Button>
        </Popconfirm>

        {/* <Popconfirm className="FooterPop" placement="topLeft" icon={null} title="请确认稿件已全部保存，返回列表界面?" onConfirm={this.confirm} onCancel={() => {}} okText="确定" cancelText="取消">
          <Button size={size}>关闭</Button>
        </Popconfirm> */}

        <Button size={size} onClick={this.confirm}>
          关闭
        </Button>
      </div>
    );
  }
}
export default Footer;
